package com.m3839.sdk.pay.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.m3839.sdk.anti.bean.ButtonBean;
import com.m3839.sdk.anti.bean.LinkBean;
import com.m3839.sdk.common.base.BaseBean;
import com.m3839.sdk.common.core.Constant;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:HykbPayLimit
 *
 * @author 陈宝阳
 * @create 2020/8/14 15:31
 */
public class PayLimit extends BaseBean<PayLimit> implements Parcelable {

  /**
   * "content": "您未满8岁，无法充值。感谢您的理解。",
   *         "tips": "小娃娃，长大点再来充钱啊",
   *         "link": {
   *             "name": "查看详情",
   *             "url": "https://cdn.h5wan.4399sj.com/static/addiction/recharge.html"
   *         },
   *         "buttons": [
   *             {
   *                 "name": "关闭",
   *                 "action": "exit_popup"
   *             }
   *         ]
   */


  /** 健康系统提示 */
  public String content;

  /** 温馨提示 */
  public String tips;

  /** 查看详情 */
  public LinkBean linkBean;

  /** 按钮内容 */
  public List<ButtonBean> buttonBeanList;

  public PayLimit(){}

  protected PayLimit(Parcel in) {
    content = in.readString();
    tips = in.readString();
    linkBean = in.readParcelable(LinkBean.class.getClassLoader());
    buttonBeanList = in.createTypedArrayList(ButtonBean.CREATOR);
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(content);
    dest.writeString(tips);
    dest.writeParcelable(linkBean, flags);
    dest.writeTypedList(buttonBeanList);
  }

  @Override
  public int describeContents() {
    return 0;
  }

  public static final Creator<PayLimit> CREATOR = new Creator<PayLimit>() {
    @Override
    public PayLimit createFromParcel(Parcel in) {
      return new PayLimit(in);
    }

    @Override
    public PayLimit[] newArray(int size) {
      return new PayLimit[size];
    }
  };

  @Override
  public PayLimit parseBean(JSONObject jsonObject) {
    if(jsonObject == null) {
      return null;
    }
    tips = jsonObject.optString(Constant.HYKB_KEY_RESPONSE_PAY_TIPS);
    content = jsonObject.optString(Constant.HYKB_KEY_RESPONSE_PAY_LIMIT_CONTENT);
    linkBean = new LinkBean().parseBean(
            jsonObject.optJSONObject(Constant.HYKB_KEY_RESPONSE_PAY_LIMIT_LINK));
    buttonBeanList = parseButtons(
            jsonObject.optJSONArray(Constant.HYKB_KEY_RESPONSE_PAY_LIMIT_BUTTONS));
    return this;
  }

  private List<ButtonBean> parseButtons(JSONArray jsonArray) {
    if(jsonArray == null) {
      return null;
    }
    List<ButtonBean> buttonBeans = new ArrayList<>();
    int len = jsonArray.length();
    for (int i = 0; i < len; i++) {
      JSONObject buttonJson = jsonArray.optJSONObject(i);
      buttonBeans.add(new ButtonBean(buttonJson));
    }
    return buttonBeans;
  }
}
